package com.mihe.core.domain;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.mihe.core.constant.HttpStatus;
import com.mihe.core.constant.ResultEnum;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @author Dirk
 * @Date 2020-07-08 10:52
 */
public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = 5585474973238010750L;

    @ApiModelProperty(name = "code", value = "状态码")
    private int code;

    @ApiModelProperty(name = "message", value = "信息语句")
    private String message;

    @ApiModelProperty(name = "data", value = "具体DTO返回")
    private T data;

    public ResponseResult() {
        this.code =ResultEnum.SUCCESS.getCode();
        this.message = ResultEnum.SUCCESS.getMessage();
        this.data=this.getDefaultData();
    }

    public ResponseResult(int code, String message) {
        this.code = code;
        this.message = message;
        this.data= this.getDefaultData();
    }

    /**
     * @param code
     * @param message
     * @param data
     * @author ssk
     */
    public ResponseResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        if (ObjectUtil.isNotNull(data)) {
            this.data = data;
        }else{
            this.getDefaultData();
        }
    }

    private T getDefaultData(){
        JSONObject json=new JSONObject();
        return (T) json;
    }

    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<T>().setData(data);
    }

    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static ResponseResult success() {
        return success("操作成功!");
    }

    /**
     * 返回成功消息
     *
     * @param message 返回内容
     * @return 成功消息
     */
    public static ResponseResult success(String message) {
        return new ResponseResult(200, message);
    }

    /**
     * 返回成功消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static <T> ResponseResult<T> success(String msg, T data) {
        return new ResponseResult<T>(HttpStatus.SUCCESS, msg, data);
    }


    public int getCode() {
        return code;
    }

    public ResponseResult<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public ResponseResult<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public ResponseResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static ResponseResult error() {
        return error("操作失败");
    }

    /**
     * 返回错误消息
     *
     * @param message 返回内容
     * @return 警告消息
     */
    public static ResponseResult error(String message) {
        return new ResponseResult(500, message);
    }

    /**
     * 返回错误消息
     *
     * @param code    状态码
     * @param message 返回内容
     * @return 警告消息
     */
    public static ResponseResult error(int code, String message) {
        return new ResponseResult(code, message);
    }

    /**
     * 返回错误消息
     *
     * @param code    状态码
     * @param message 返回内容
     * @return 警告消息
     */
    public static <T> ResponseResult<T> error(int code, String message, T data) {
        return new ResponseResult<T>(code, message, data);
    }
}
